﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Diagnostics;
using System.IO;
using BHXH_NT.XML;
using Microsoft.VisualBasic;
using System.Transactions;
using BHXH_NT.Properties;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using CrystalDecisions.ReportSource;
using C1.Win.C1FlexGrid;
using System.Collections;
using BHXH_NT.Data;

namespace BHXH_NT
{
    public partial class FrmInToBia : Form
    {
        private string connectionString1 = global::BHXH_NT.Properties.Settings.Default.MISBHXH_NTConnectionString1;
        InToBia dsTobia = new InToBia();
        Random rnd = new Random();
        public FrmInToBia()
        {
            InitializeComponent();
        }
        #region "Private Method"
        private int CheckSoLuongToBia(int soluong)
        {
            string strMonth;
            int intSonhap = 0, intSoxuat = 0;
            strMonth = Convert.ToString(Convert.ToInt32(ModuleBHXH.GetMonth()) - 1);
            if (strMonth.Substring(4) == "00")
            {
                strMonth = Convert.ToString(Convert.ToInt32(strMonth.Substring(0, 4)) - 1);
                strMonth = strMonth + "12";
            }
            SqlDataReader reader;
            using (SqlConnection connection =
              new SqlConnection(connectionString1))
            {
                string queryString;
                queryString = " exec sp_executesql N'SELECT SPACE(6) As Thang, Makho, Mavt, Cuoiky As Dauky, Cuoiky*0 As Nhap, Cuoiky*0 As Xuat, "
                + " Cuoiky, Ma_Quan, SPACE(1) As Loai  FROM SODUVT "
                + " WHERE Thang = @P1  AND Ma_Quan=@P2  AND Makho = @P3  AND Mavt = @P4 ', "
                + " N'@P1 varchar(6),@P2 varchar(2),@P3 varchar(10),@P4 varchar(2)', '" + strMonth + "', '01', '" + ModuleBHXH.strUser + "', 'BT'";

                SqlCommand command = connection.CreateCommand();
                command.CommandText = queryString;
                connection.Open();
                reader = command.ExecuteReader();
                while (reader.Read())
                {
                    intSonhap = Convert.ToInt32(reader["Cuoiky"]);
                }
                reader.Close();
                queryString = " exec sp_executesql N'SELECT b.Makho, b.Mavt, a.NX,  SUM(CASE WHEN a.NX=''N'' THEN b.Soluong ELSE 0 END) As Nhap,"
                + " SUM(CASE WHEN a.NX=''X'' THEN b.Soluong ELSE 0 END) As Xuat  FROM CTUVT_HDR a, "
                + " CTUVT_DTL b WHERE a.Keysl=b.Keysl  AND a.Ngayct BETWEEN @P1  AND @P2   AND b.Makho = @P3  "
                + " AND b.Mavt = @P4  GROUP BY b.Makho, b.Mavt, a.NX ', N'@P1 datetime,@P2 datetime,@P3 varchar(10),@P4 varchar(2)', "
                + " '" + ModuleBHXH.strMonths.Substring(4, 2) + "/01/" + ModuleBHXH.strMonths.Substring(0, 4) + "', '" + ModuleBHXH.strDate + "', '" + ModuleBHXH.strUser + "', 'BT'";
                command.CommandText = queryString;
                reader = command.ExecuteReader();
                while (reader.Read())
                {
                    intSonhap = intSonhap + Convert.ToInt32(reader["Nhap"]);
                    intSoxuat = intSoxuat + Convert.ToInt32(reader["Xuat"]);
                }
                reader.Close();
                queryString = " exec sp_executesql N'SELECT b.Manv As Makho, b.Mavt, ''N'' As NX,  SUM(b.Soluong) As Nhap  "
                + " FROM CTUVT_HDR a, CTUVT_DTL b WHERE a.Keysl=b.Keysl  AND a.Ngayct BETWEEN @P1  AND @P2   "
                + " AND a.NX=''X'' AND b.Manv<>''''  AND b.Manv = @P3  AND b.Mavt = @P4  GROUP BY b.Manv, b.Mavt ', "
                + " N'@P1 datetime,@P2 datetime,@P3 varchar(10),@P4 varchar(2)', "
                + "'" + ModuleBHXH.strMonths.Substring(4, 2) + "/01/" + ModuleBHXH.strMonths.Substring(0, 4) + "', '" + ModuleBHXH.strDate + "', '" + ModuleBHXH.strUser + "', 'BT'";
                command.CommandText = queryString;
                reader = command.ExecuteReader();
                while (reader.Read())
                {
                    intSonhap = intSonhap + Convert.ToInt32(reader["Nhap"]);
                }
                reader.Close();
                queryString = " exec sp_executesql N'SELECT Makho, Mavt, NX,  SUM(CASE WHEN NX=''N'' THEN Soluong ELSE 0 END) As Nhap,"
                + " SUM(CASE WHEN NX=''X'' THEN Soluong ELSE 0 END) As Xuat  FROM CTUVT_SD "
                + " WHERE Ngayct BETWEEN @P1  AND @P2   AND Makho = @P3  AND Mavt = @P4  GROUP BY Makho, Mavt, NX ', "
                + " N'@P1 datetime,@P2 datetime,@P3 varchar(10),@P4 varchar(2)', "
                + "'" + ModuleBHXH.strMonths.Substring(4, 2) + "/01/" + ModuleBHXH.strMonths.Substring(0, 4) + "', '" + ModuleBHXH.strDate + "', '" + ModuleBHXH.strUser + "', 'BT'";
                command.CommandText = queryString;
                reader = command.ExecuteReader();
                while (reader.Read())
                {
                    intSonhap = intSonhap + Convert.ToInt32(reader["Nhap"]);
                    intSoxuat = intSoxuat + Convert.ToInt32(reader["Xuat"]);
                }
                reader.Close();
                queryString = " exec sp_executesql N'SELECT Makho, ''BT'' As Mavt, ''X'' As NX,  SUM(Soluong) As Xuat  "
                + " FROM CTUVT_SD WHERE Ngayct BETWEEN @P1  AND @P2  AND NX=''N'' AND (Mavt=''BH'' OR Mavt=''BB'') AND Makho = @P3  GROUP BY Makho ', "
                + " N'@P1 datetime,@P2 datetime,@P3 varchar(10)', "
                + "'" + ModuleBHXH.strMonths.Substring(4, 2) + "/01/" + ModuleBHXH.strMonths.Substring(0, 4) + "', '" + ModuleBHXH.strDate + "', '" + ModuleBHXH.strUser + "'";
                command.CommandText = queryString;
                reader = command.ExecuteReader();
                while (reader.Read())
                {
                    intSoxuat = intSoxuat + Convert.ToInt32(reader["Xuat"]);
                }
                reader.Close();
            }
            return intSonhap - intSoxuat;
        }
        #endregion

        #region "private Event"
        private void FrmInToBia_Load(object sender, EventArgs e)
        {
            dtTungay.Value = DateTime.Now.Day.ToString() + "/" + DateTime.Now.Month.ToString() + "/" + DateTime.Now.Year.ToString();
        }
        private void btnSolieu_Click(object sender, EventArgs e)
        {
            string strSearch = "";
            if (cboNV.Text != "") // truong hop loc in theo nhan vien
            {
                strSearch = " AND DSNV LIKE '%" + cboNV.Text + "%'";
            }
            using (SqlConnection connection =
                new SqlConnection(connectionString1))
            {
                SqlCommand command = connection.CreateCommand();
                string queryString = "select  b.soct,* from dbo.INSO_DTL a "
                + " inner join MISBHXH_NT.dbo.CAPSO b on a.Madvi = b.Madvi "
                + " and CAST(right(rtrim(a.sobhxh),6) as int) >= CAST(rtrim(tuso) as int) "
                + " and CAST(right(rtrim(a.sobhxh),6) as int) <= cast(rtrim(denso) as int) "
                + " LEFT JOIN MISBHXH_NT.dbo.DMDONVI c ON a.Madvi=c.Madvi "
                + " where "
                + " ngaycap = '" + ModuleBHXH.FormatDate(dtTungay.Text) + "' and a.Usercode = '" + ModuleBHXH.strUser + "' "
                + " and userin = '' and ngayin is null and b.nam = '" + dtTungay.Text.Substring(6) + "'" + strSearch
                + " ORDER BY sobhxh ";

                //" + dtTungay.Text.Substring(6) + "

                command.CommandText = queryString;
                connection.Open();

                SqlDataReader reader = command.ExecuteReader();
                int i = 1,j=1 ;
                FlxDetail.Rows.Count = 1;
                FlxDetail.Tree.Column = 3;
                while (reader.Read())
                {
                    if (FlxDetail.Rows[i - 1]["sophieu"].ToString() != reader["soct"].ToString())
                    {
                        FlxDetail.Rows.InsertNode(i, 1);
                        j = 1;
                    }
                    else
                    {
                        FlxDetail.Rows.InsertNode(i, 2);
                    }
                    FlxDetail.SetCellCheck(i, 3, CheckEnum.Checked);
                    FlxDetail.Rows[i]["STT1"] = i;
                    
                    FlxDetail.Rows[i]["STT"] = j;
                    FlxDetail.Rows[i]["sophieu"] = reader["SOCT"];
                    FlxDetail.Rows[i]["Madvi"] = reader["Madvi"];
                    FlxDetail.Rows[i]["diachidvi"] = reader["DIACHIDVI"];
                    if (FlxDetail.Rows[i]["diachidvi"].ToString().Length > 40)
                    {
                        FlxDetail.Rows[i]["diachidvi"] = FlxDetail.Rows[i]["diachidvi"].ToString().Insert(FlxDetail.Rows[i]["diachidvi"].ToString().Substring(0, 40).LastIndexOf(" ") + 1, "\n");
                    }
                    FlxDetail.Rows[i]["Sobhxh"] = reader["SOBHXH"];
                    FlxDetail.Rows[i]["hoten"] = reader["Ho"].ToString().Trim() + " " + reader["Ten"].ToString().Trim();
                    FlxDetail.Rows[i]["ngaysinh"] = reader["ngaysinh"];
                    FlxDetail.Rows[i]["gioitinh"] = reader["gioitinh"];
                    FlxDetail.Rows[i]["Ngaycap"] = reader["Ngaycap"];
                    FlxDetail.Rows[i]["nguyenquan"] = reader["noikhai"];
                    FlxDetail.Rows[i]["diachi"] =  ModuleBHXH.ChangeToCamel(reader["diachi"].ToString());
                    FlxDetail.Rows[i]["diachi1"] = FlxDetail.Rows[i]["diachi"];
                    if (FlxDetail.Rows[i]["diachi"].ToString().Length > 34)
                    {
                        FlxDetail.Rows[i]["diachi"] = FlxDetail.Rows[i]["diachi"].ToString().Insert(FlxDetail.Rows[i]["diachi"].ToString().Substring(0, 34).LastIndexOf(" ") + 1, "\n");
                    }
                    FlxDetail.Rows[i]["CMND"] = reader["socmnd"];
                    FlxDetail.Rows[i]["NGAYCMND"] = reader["NGAYCMND"];
                    FlxDetail.Rows[i]["noicap"] = reader["noicap"];
                    FlxDetail.Rows[i]["Noicapso"] = ModuleBHXH.ChangeToCamel(reader["NOICAPSO"].ToString());
                    if (FlxDetail.Rows[i]["Noicapso"].ToString().Length > 38)
                    {
                        FlxDetail.Rows[i]["Noicapso"] = FlxDetail.Rows[i]["Noicapso"].ToString().Insert(FlxDetail.Rows[i]["Noicapso"].ToString().Substring(0, 38).LastIndexOf(" ") + 1, "\n");
                    }
                    //FlxDetail.Rows[i]["Noicapso"] = reader["NOICAPSO"].ToString();
                    FlxDetail.Rows[i]["ho"] = reader["ho"];
                    FlxDetail.Rows[i]["ten"] = reader["ten"];
                    FlxDetail.Rows[i]["KEYSL"] = reader["KEYSL"];
                    FlxDetail.Rows[i]["KEYSL1"] = reader["KEYP"];
                    FlxDetail.Rows[i]["DSNV"] = reader["DSNV"];
                    j++;
                    i++;
                }
                reader.Close();
            }
            btnSLton.Enabled = false;
            btnTrang2.Enabled = false;
            BtnTrang1.Enabled = true;
        }
        private void BtnTrang1_Click(object sender, EventArgs e)
        {
            
            DataRow newRow = null;
            dsTobia.Clear();
            for (int i = 1; i < FlxDetail.Rows.Count; i++)
            {
                if (FlxDetail.GetCellCheck(i, 3) == CheckEnum.Checked)
                {
                    newRow = dsTobia.Tables[0].NewRow();
                    newRow["Madvi"] = FlxDetail.Rows[i]["Madvi"];
                    newRow["Sophieu"] = FlxDetail.Rows[i]["Sophieu"];
                    newRow["SOBHXH"] = FlxDetail.Rows[i]["Sobhxh"] ;
                    newRow["Hoten"]=FlxDetail.Rows[i]["hoten"] ;
                    newRow["ngaysinh"] = FlxDetail.Rows[i]["ngaysinh"].ToString().Substring(0,10);
                    newRow["gioitinh"] = FlxDetail.Rows[i]["gioitinh"];
                    if (newRow["gioitinh"].ToString() == "1")
                    {
                        newRow["gioitinh"] = "Nam";
                    }
                    else
                    {
                        newRow["gioitinh"] = "N÷";
                    }
                   // newRow["Ngaycap"] = FlxDetail.Rows[i]["Ngaycap"];
                    newRow["nguyenquan"] = FlxDetail.Rows[i]["nguyenquan"];
                    newRow["diachi"] = FlxDetail.Rows[i]["diachi"] ;
                    newRow["diachi1"] = FlxDetail.Rows[i]["diachi1"];
                    newRow["CMND"] = FlxDetail.Rows[i]["CMND"];
                    newRow["NGAYCMND"] = FlxDetail.Rows[i]["NGAYCMND"].ToString().Substring(0, 10);
                    newRow["noicap"] = FlxDetail.Rows[i]["noicap"] ;
                    newRow["NOICAPSO"] = FlxDetail.Rows[i]["Noicapso"] ;
                    newRow["diachiDVI"] = FlxDetail.Rows[i]["DIACHIDVI"];
                    newRow["KEYSL"] = FlxDetail.Rows[i]["KEYSL"];
                    newRow["KEYSL1"] = FlxDetail.Rows[i]["KEYSL1"];

                    dsTobia.Tables[0].Rows.Add(newRow);
                }
            }
            Report f = new Report();
            ReportDocument cr = new ReportDocument();
            cr.Load("crpTobiaTrang1.rpt");
            cr.SetDataSource(dsTobia);
            f.crpView.ReportSource = cr;
            f.Show();

            btnSLton.Enabled = false;
            btnTrang2.Enabled = true;
            
        }
        private void btnTrang2_Click(object sender, EventArgs e)
        {
            Report f = new Report();
            ReportDocument cr = new ReportDocument();
            cr.Load("crpTobiaTrang2.rpt");
            cr.SetDataSource(dsTobia);
            foreach (CrystalDecisions.CrystalReports.Engine.FormulaFieldDefinition thisFormulaField in cr.DataDefinition.FormulaFields)
            {
                switch (thisFormulaField.Name)
                {
                    case "User":
                        thisFormulaField.Text = "'" + ModuleBHXH.strUser + "'";
                        break;
                    case "Ngay":
                        thisFormulaField.Text = "' Ngµy " + String.Format("{0:00}", DateTime.Now.Day) + " th¸ng " + String.Format("{0:00}", DateTime.Now.Month) + " n¨m " + DateTime.Now.Year + "'";
                        break;

                }
            }
            f.crpView.ReportSource = cr;
            f.Show();
            btnSLton.Enabled = true;
        }
        private void btnSLton_Click(object sender, EventArgs e)
        {
            string queryString;
            string strMadviTam = "";
            int soluong = 0;
            using (SqlConnection connection =
               new SqlConnection(connectionString1))
            {
                SqlCommand command = connection.CreateCommand();
                connection.Open();
                for (int i = 0; i < dsTobia.Tables[0].Rows.Count; i++)
                {
                    queryString = "exec sp_executesql N'UPDATE dbo.INSO_DTL SET DAIN=@P1,NGAYIN=@P2,USERIN=@P3 "
                    + " WHERE KEYSL=@P4 AND KEYP=@P5', N'@P1 smallint,@P2 smalldatetime,@P3 varchar(10),@P4 char(22),"
                    + " @P5 char(8)', 1, '" + ModuleBHXH.GetDate() + "', '" + ModuleBHXH.strUser + "', '" + dsTobia.Tables[0].Rows[i]["KEYSL"].ToString().Trim() + "', '"+ dsTobia.Tables[0].Rows[i]["KEYSL1"].ToString().Trim() + "'";
                    command.CommandText = queryString;
                    command.ExecuteNonQuery();
                }
                                
                for (int i = 0; i < dsTobia.Tables[0].Rows.Count; i++)
                {
                    if (strMadviTam != dsTobia.Tables[0].Rows[i]["Madvi"].ToString())
                    {
                        if (soluong != 0 && CheckSoLuongToBia(soluong) > 0)
                        {
                            queryString = "exec sp_executesql N'INSERT INTO dbo.CTUVT_SD "
                            + " (SOCT,NGAYCT,NX,MAKHO,MAVT,SOLUONG,TUSO,DENSO,NOIDUNG,USERCODE,KEYSL) "
                            + " VALUES (@P1,@P2,@P3,@P4,@P5,@P6,@P7,@P8,@P9,@P10,@P11)', "
                            + " N'@P1 char(10),@P2 smalldatetime,@P3 char(1),@P4 char(10),@P5 char(10),@P6 numeric(18,0),"
                            + " @P7 varchar(15),@P8 varchar(15),@P9 varchar(100),@P10 char(10),@P11 char(20)', "
                            + " '          ', '" + ModuleBHXH.GetDate() + " ', 'X', '" + ModuleBHXH.strUser + "', 'BT        ', " + soluong + ", "
                            + "'               ', '               ', 'CÊp tê b×a cho ®¬n vÞ " + strMadviTam + "', "
                            + " '" + ModuleBHXH.strUser + "', '" + ModuleBHXH.CreateRunninNo(rnd) + "'";
                                                    
                            command.CommandText = queryString;
                            command.ExecuteNonQuery();
                        }
                        strMadviTam = dsTobia.Tables[0].Rows[i]["Madvi"].ToString();
                        soluong = 1;
                    }
                    else
                    {
                        soluong++;
                    }
                }
                if (soluong != 0 && CheckSoLuongToBia(soluong) > 0)
                {
                    queryString = "exec sp_executesql N'INSERT INTO dbo.CTUVT_SD "
                    + " (SOCT,NGAYCT,NX,MAKHO,MAVT,SOLUONG,TUSO,DENSO,NOIDUNG,USERCODE,KEYSL) "
                    + " VALUES (@P1,@P2,@P3,@P4,@P5,@P6,@P7,@P8,@P9,@P10,@P11)', "
                    + " N'@P1 char(10),@P2 smalldatetime,@P3 char(1),@P4 char(10),@P5 char(10),@P6 numeric(18,0),"
                    + " @P7 varchar(15),@P8 varchar(15),@P9 varchar(100),@P10 char(10),@P11 char(20)', "
                    + " '          ', '" + ModuleBHXH.GetDate() + " ', 'X', '" + ModuleBHXH.strUser + "', 'BT        ', " + soluong + ", "
                    + "'               ', '               ', 'CÊp tê b×a cho ®¬n vÞ " + strMadviTam + "', "
                    + " '" + ModuleBHXH.strUser + "', '" + ModuleBHXH.CreateRunninNo(rnd) + "'";
                    command.CommandText = queryString;
                    command.ExecuteNonQuery();
                }
                else if (soluong != 0)
                {
                    MessageBox.Show("xuất quá số lượng tờ bìa");
                }
            }
            dsTobia.Clear();
            FlxDetail.Rows.Count = 1;
            btnSLton.Enabled = false;
            BtnTrang1.Enabled = false;
            btnTrang2.Enabled = false;
            
        }
        private void btnDS_Click(object sender, EventArgs e)
        {
            Report f = new Report();
            ReportDocument cr = new ReportDocument();
            cr.Load("crpGroupDScapso.rpt");
            cr.SetDataSource(dsTobia);
            foreach (CrystalDecisions.CrystalReports.Engine.FormulaFieldDefinition thisFormulaField in cr.DataDefinition.FormulaFields)
            {
                switch (thisFormulaField.Name)
                {
                    case "truongphong":
                        thisFormulaField.Text = "' §íi Duy Xu©n '";
                        break;
                    case "nguoilap":
                        thisFormulaField.Text = "'" + global::BHXH_NT.Properties.Settings.Default.Nguoilap + "'";
                        break;
                    
                }
            }
            f.crpView.ReportSource = cr;
            f.Show();
            btnSLton.Enabled = true;
        }
        private void btnBB_Click(object sender, EventArgs e)
        {
            DataRow newRow = null;
            string strSearch = "";
            if (cboNV.Text != "") // truong hop loc in theo nhan vien
            {
                strSearch = " AND DSNV LIKE '%" + cboNV.Text + "%'";
            }
            using (SqlConnection connection =
                new SqlConnection(connectionString1))
            {
                SqlCommand command = connection.CreateCommand();
                string queryString = "select b.soct,b.tuso,b.denso,count(sobhxh) as tongbia   , "
                + " c.Madvi,c.Tendvi,c.diachi,c.ghichu, "
                + " (select count(sobhxh) as tongtr from dbo.INTR_HDR "
                + " where  Cast(sobhxh as float) <= Cast(('" + Convert.ToString(Convert.ToInt64(Properties.Settings.Default.MaTinh + "13")) + "' +  b.denso) as float) and "
                + " Cast(sobhxh as float) >= Cast(('" + Convert.ToString(Convert.ToInt64(Properties.Settings.Default.MaTinh + dtTungay.Text.Substring(8))) + "' +  b.tuso) as float)) as tongtr from dbo.INSO_DTL a  inner join MISBHXH_NT.dbo.CAPSO b "
                + " on a.Madvi = b.Madvi  and CAST(right(rtrim(a.sobhxh),6) as int) >= CAST(rtrim(tuso) as int)  and "
                + " CAST(right(rtrim(a.sobhxh),6) as int) <= cast(rtrim(denso) as int) "
                + " inner join MISBHXH_NT.dbo.DMDONVI c on c.Madvi = b.Madvi" 
                + " where "
                + " ngaycap = '" + ModuleBHXH.FormatDate(dtTungay.Text) + "' and a.Usercode = '" + ModuleBHXH.strUser + "' "
                + " and userin = '' and ngayin is null and b.nam = '" + dtTungay.Text.Substring(6) + "'" + strSearch
                + " group by  b.soct,b.tuso,b.denso, c.Madvi,c.Tendvi,c.diachi,c.ghichu "
                + " ORDER BY soct ";
                //dtTungay.Text.Substring(8)
                //dtTungay.Text.Substring(6)
                command.CommandText = queryString;
                command.CommandTimeout = 120;
                connection.Open();

                SqlDataReader reader = command.ExecuteReader();
                while (reader.Read())
                {
                    newRow = dsTobia.Tables["BB"].NewRow();
                    newRow["sophieu"] = reader["soct"];
                    newRow["tuso"] = Convert.ToString(Convert.ToInt64(Properties.Settings.Default.MaTinh + 14)) + reader["tuso"].ToString();
                    newRow["denso"] = Convert.ToString(Convert.ToInt64(Properties.Settings.Default.MaTinh + 14)) + reader["denso"].ToString();
                    newRow["Madvi"] = reader["Madvi"];
                    newRow["Tendvi"] = reader["Tendvi"];
                    newRow["tongbia"] = reader["tongbia"];
                    newRow["tongbiachu"] = ModuleBHXH.Doc_tien_sang_tieng_viet(Convert.ToDouble(newRow["tongbia"])) + "b×a sæ" ;
                    newRow["diachidvi"] = reader["diachi"];
                    if (reader["ghichu"].ToString() != "" && reader["ghichu"].ToString().IndexOf("-") > 0)
                    {
                        newRow["nguoidaidien"] = Converter.TCVN3ToUnicode(reader["ghichu"].ToString().Substring(0, reader["ghichu"].ToString().IndexOf("-"))) ;
                        newRow["chucdanh"] =  Converter.TCVN3ToUnicode(reader["ghichu"].ToString().Substring(reader["ghichu"].ToString().IndexOf("-") + 1)) ; 
                    }
                    newRow["tongtoroi"] = reader["tongtr"];

                    newRow["tongtoroichu"] =  ModuleBHXH.Doc_tien_sang_tieng_viet(Convert.ToDouble(newRow["tongtoroi"])) + "tê rêi" ;
                    dsTobia.Tables["BB"].Rows.Add(newRow);
                }
                reader.Close();
                Report f = new Report();
                ReportDocument cr = new ReportDocument();
                cr.Load("crpGroupBBGiaonhan.rpt");
                cr.SetDataSource(dsTobia);
                 
                foreach (CrystalDecisions.CrystalReports.Engine.FormulaFieldDefinition thisFormulaField in cr.DataDefinition.FormulaFields)
                {
                    switch (thisFormulaField.Name)
                    {
                       
                        case "TenCty":
                            thisFormulaField.Text = "'" + Settings.Default.Congty + "'";
                            break;
                        case "Diachi":
                            thisFormulaField.Text = "'" + Settings.Default.DiaChi + "'";
                            break;
                        case "truongphong":
                            thisFormulaField.Text = "'" + Settings.Default.Thutruong + "'";
                            break;

                    }
                }
                f.crpView.ReportSource = cr;
                f.Show();
                btnSLton.Enabled = true;
            }
        }
        private void FlxDetail_Click(object sender, EventArgs e)
        {
            if (FlxDetail.Selection.r1 < 0) { return; }
            if (FlxDetail.Rows[FlxDetail.Selection.r1 - 1]["sophieu"] == null) { return; }
            if (FlxDetail.Rows[FlxDetail.Selection.r1]["sophieu"].ToString() != FlxDetail.Rows[FlxDetail.Selection.r1 - 1]["sophieu"].ToString() &&
                FlxDetail.GetCellCheck(FlxDetail.Selection.r1, 3) == CheckEnum.Unchecked)
            {
                int i = 1;
                while (FlxDetail.Selection.r1 + i < FlxDetail.Rows.Count && FlxDetail.Rows[FlxDetail.Selection.r1]["sophieu"].ToString() == FlxDetail.Rows[FlxDetail.Selection.r1 + i]["sophieu"].ToString())
                {
                    FlxDetail.SetCellCheck(FlxDetail.Selection.r1 + i, 3, CheckEnum.Unchecked);
                    i++;

                }

            }
            else if (FlxDetail.Rows[FlxDetail.Selection.r1]["sophieu"].ToString() != FlxDetail.Rows[FlxDetail.Selection.r1 - 1]["sophieu"].ToString() &&
                FlxDetail.GetCellCheck(FlxDetail.Selection.r1, 3) == CheckEnum.Checked)
            {
                int i = 1;
                while (FlxDetail.Selection.r1 + i < FlxDetail.Rows.Count && FlxDetail.Rows[FlxDetail.Selection.r1]["sophieu"].ToString() == FlxDetail.Rows[FlxDetail.Selection.r1 + i]["sophieu"].ToString())
                {
                    FlxDetail.SetCellCheck(FlxDetail.Selection.r1 + i, 3, CheckEnum.Checked);
                    i++;

                }

            }
        }
        #endregion

    }
}
